<template>
  <div class="amount-log-page" v-loading="loading" element-loading-text="加载中...">
    <div class="amount-log-title">
      <div class="amount-log-title-left">
        <el-button type="primary" @click="goback">
          <template #icon>
            <IEpBack />
          </template>返回
        </el-button>
      </div>
      <div class="amount-log-title-right"></div>
    </div>
    <div class="amount-log-table">
      <el-table border stripe :data="tableData" :height="store.tableHeight - 40">
        <el-table-column prop="goods_code" label="商品编号" />
        <el-table-column prop="goods_spec" label="商品规格"> </el-table-column>
        <el-table-column prop="goods_stock" label="入库数量"> </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="170" />
        <el-table-column label="操作" width="90" align="center">
          <template #default="{ row }">
            <el-button type="danger" link @click="delFun(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-div">
        <div></div>
        <el-pagination v-model:currentPage="currentPage" v-model:page-size="pageSize" :page-sizes="[20, 50, 100, 200]"
          layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
        <div></div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { useCounterStore } from "@/stores/counter.js";
import { goodsLogApi as listApi, delGoodsLogApi as delApi } from "@/plugins/api/shopping/manage";

const store = useCounterStore();
const router = useRouter();
const route = useRoute();
let tableData = ref([]);
let loading = ref(false);
const currentPage = ref(1);
const pageSize = ref(20);
const total = ref(0);
const handleSizeChange = (val) => {
  pageSize.value = val;
  getData();
};
const handleCurrentChange = (val) => {
  currentPage.value = val;
  getData();
};
function goback() {
  router.back();
}
let goodsId = ref(null);
function getData() {
  loading.value = true;

  listApi({
    goods_id: goodsId.value,
    page: currentPage.value,
    pageSize: pageSize.value,
  })
    .then((data) => {
      tableData.value = data.list;
      total.value = data.page.total;
      loading.value = false;
    })
    .catch(() => {
      loading.value = false;
    });
}
function delFun(row) {
  ElMessageBox.confirm("确认删除，是否继续?", "提示", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      console.log(row);
      delApi({
        ids: row.id.toString(),
      }).then(() => {
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
        getData();
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消。",
      });
    });
}

onMounted(() => {
  if (route.query.goodsId) {
    goodsId.value = parseInt(route.query.goodsId);
    getData();
  } else {
    router.back();
  }
});
</script>
<style scoped lang="scss">
.amount-log-page {
  width: 100%;
  height: 100%;

  .amount-log-title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #eeeeee;
    padding-bottom: 10px;

    .amount-log-title-left {
      font-size: 18px;
      font-weight: 700;
      color: #172b4d;

      img {
        vertical-align: middle;
      }

      span {
        vertical-align: middle;
        margin-left: 5px;
      }
    }
  }

  .amount-log-table {
    padding: 10px;
    box-sizing: border-box;

    .pagination-div {
      display: flex;
      justify-content: center;
      margin-top: 10px;
    }
  }
}
</style>
